#include <QCoreApplication>
#include <QDebug>
#include "UdpReceiver.h"
#include "ImageProcessor.h"
#include "DataSender.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    // 初始化FFmpeg
    av_register_all();
    avcodec_register_all();
    
    // 创建组件
    UdpReceiver receiver;
    ImageProcessor processor;
    DataSender sender;
    
    // 配置端口和地址
    receiver.setPort(5555);  // 接收灰度图像的端口
    sender.setDestination("192.168.1.100", 6666);  // 发送编码后数据的目标地址
    
    // 连接信号槽
    QObject::connect(&receiver, &UdpReceiver::dataReceived, 
                     &processor, &ImageProcessor::processImageData);
    QObject::connect(&processor, &ImageProcessor::frameEncoded,
                     &sender, &DataSender::sendData);
    QObject::connect(&receiver, &UdpReceiver::errorOccurred,
                     [](const QString &msg) { qWarning() << "接收错误:" << msg; });
    QObject::connect(&processor, &ImageProcessor::errorOccurred,
                     [](const QString &msg) { qWarning() << "处理错误:" << msg; });
    QObject::connect(&sender, &DataSender::errorOccurred,
                     [](const QString &msg) { qWarning() << "发送错误:" << msg; });
    
    // 启动线程
    receiver.start();
    
    qInfo() << "系统启动完成，开始接收和处理图像...";
    
    return a.exec();
}
